﻿using System;
using System.Xml;

namespace MvcForum.Net.Utils
{
    public class Localizer
    {
        private XmlDocument _doc;

        public XmlDocument XmlDocument
        {

            get { return _doc; }

        }

        public Localizer(string _filePath)
        {
            LoadFile(_filePath);
        }

        private void LoadFile(string _filePath)
        {


            //string _filePath = HttpContext.Current.Server.MapPath("languages/" + ForumConfigs.Instance.DefaultLanguage + ".xml");

            if (_filePath == "" || !System.IO.File.Exists(_filePath))
                throw (new ApplicationException("Invalid language file " + _filePath));

            if (_doc == null)
                _doc = new XmlDocument();

            _doc.Load(_filePath);

        }


        /// <summary>
        ///   Retrieves the XPath string used for retrieving a section from the XML file. </summary>
        /// <returns>
        ///   An XPath string. </returns>
        /// <seealso cref="GetEntryPath" />
        private static string GetSectionsPath(string section)
        {
            return "page[@name=\"" + section + "\"]";
        }

        /// <summary>
        ///   Retrieves the XPath string used for retrieving an entry from the XML file. </summary>
        /// <returns>
        ///   An XPath string. </returns>
        /// <seealso cref="GetSectionsPath" />
        private static string GetEntryPath(string entry)
        {
            return "entry[@name=\"" + entry + "\"]";
        }


        private string SelectNode(string _path,string _entry)
        {
            try
            {

                var _root = _doc.DocumentElement;
                var _entryNode = _root.SelectSingleNode(string.Format("{0}/{1}",_path,GetEntryPath(_entry)));
                return _entryNode.InnerText;


            }
            catch (Exception)
            {

                return null;
            }
            
        }

        public string GetGlobalValue(string _entry)
        {
            return SelectNode("global", _entry);

        }

        public string GetMessage(string _entry)
        {

            return SelectNode("messages", _entry);

        }

        public string GetDateValue(string _entry)
        {

            return SelectNode("datetime", _entry);
            
            //try
            //{

            //    XmlElement root = _doc.DocumentElement;

            //    XmlNode entryNode = root.SelectSingleNode("datetime/" + GetEntryPath(entry));
            //    return entryNode.InnerText;
            //}
            //catch
            //{
            //    return null;
            //}

        }

        public string GetValue(string page, string entry)
        {
           
            try
            {
               
                XmlElement root = _doc.DocumentElement;

                XmlNode entryNode = root.SelectSingleNode(GetSectionsPath(page) + "/" + GetEntryPath(entry));
                return entryNode.InnerText;
            }
            catch
            {
                return null;
            }
        }

    }



}
